define(['app/mdSheQuHospitalBase/hosLog/hosLog.model', 'app/mdSheQuHospitalBase/hosLog/hosLog.service', 'hr.common'
], function (Model, service, common
) {
    function initialize(){
        var model = new Model();
        var initParam = model.tableModel.param();

        /**
         * 查询
         */
        model.searchData = function(data, event){
            var param = '';
            if(initParam) {
                param = initParam + '&' + param;
            }
            data.tableModel.param(param);
            model.tableModel.loadData(true);
        };

        /**
         * 详细
         */
        model.showData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.hosLogModel = common.mappingData(common.extend(viewModel.hosLogModel, data));
            var dia = dialog({
                id: "showHosLogDialog",
                url: '/mdSheQuHospitalBase/hosLog/hosLog.show.tmpl.html',
                width: '600px',
                title: '日志表基本信息',
                okValue: '关闭',
                ok: function () {
 
                },
                onshow: function () {
 
                }
            });
            common.showModalDialog(dia, viewModel);
        };

        /**
         * 增加
         */
        model.addData = function(){
            var viewModel = new Model().dialogModel;
            modifyData(viewModel, '新建');
            model.tableModel.loadData(true);
        };

        /**
         * 修改
         */
        model.updateData = function (data, event) {
            var viewModel = new Model().dialogModel;
            viewModel.hosLogModel = common.mappingData(common.extend(viewModel.hosLogModel, data), []);
            modifyData(viewModel, '编辑');
        };

        /**
         * 增加或修改
         */
        function modifyData(viewModel, titlePrefix){
            var dia = dialog({
                id: 'saveHosLogDialog',
                url: '/mdSheQuHospitalBase/hosLog/hosLog.save.tmpl.html',
                width: '600px',
                title: titlePrefix + '日志表',
                okValue: '确定',
                cancelValue: '取消',
                ok: function () {
                    if(common.isErrors(viewModel.hosLogModel)){
                        var param = ko.mapping.toJSON(viewModel.hosLogModel);
                        service.saveData(param).success(function(data){
                            common.showAlertDialog('数据保存成功!', 'success');
                            model.tableModel.loadData(true);
                        });
                    }else{
                        return false;
                    }
                },
                cancel: function () {},
                onshow: function(){

                }
            });
            common.showModalDialog(dia, viewModel);
        }

        /**
         * 删除
         */
        model.deleteData = function (data, event) {
            common.showConfirmDialog('确定要删除吗?', function () {
                service.deleteDataById(data.id).success(function(data){
                    common.showAlertDialog('数据删除成功!', 'success');
                    model.tableModel.loadData(true);
                });
            });
        };

        return model;
    }
    return {
        initialize: initialize
    };
});